﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Windows.Storage;
using Windows.UI.Xaml.Media.Imaging;

namespace TT.CIT.Common
{
    public class TTSettings
    {
        // Our settings
        ApplicationDataContainer settings;

        // The key names of our settings
        const string NFCSettingKeyName = "NFC";
        const string GPSSettingKeyName = "GPS";
        const string Predio9SettingKeyName = "Predio9";
        const string Predio16SettingKeyName = "Predio16";
        const string Predio23ASettingKeyName = "Predio23A";
        const string Predio23BSettingKeyName = "Predio23B";
        const string UsuarioSettingKeyName = "Usuario";
        const string SenhaSettingKeyName = "Senha";
        const string FotoSettingKeyName = "Foto";
        const string LastCheckinSettingKeyName = "UltimoCheckin";
        const string HorarioEntradaSettingKeyName = "HorarioEntrada";
        const string HorarioSaidaSettingKeyName = "HorarioSaida";

        // The default value of our settings
        const bool NFCSettingDefault = true;
        const bool GPSSettingDefault = false;
        const bool Predio9SettingDefault = true;
        const bool Predio16SettingDefault = false;
        const bool Predio23ASettingDefault = false;
        const bool Predio23BSettingDefault = false;
        const string UsuarioSettingDefault = "";
        const string SenhaSettingDefault = "";
        const byte[] FotoSettingDefault = null;
        const string LastCheckinSettingDefault = null;
        const string HorarioEntradaSettingDefault = "00:00";
        const string HorarioSaidaSettingDefault = "00:00";

        /// <summary>
        /// Constructor that gets the application settings.
        /// </summary>
        public TTSettings()
        {
            // Get the settings for this application.
            settings = Windows.Storage.ApplicationData.Current.LocalSettings;
        }

        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            // If the key exists
            if (settings.Values.ContainsKey(Key))
            {
                // If the value has changed
                if (settings.Values[Key] != value)
                {
                    // Store the new value
                    settings.Values[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                settings.Values.Add(Key, value);
                valueChanged = true;
            }
            return valueChanged;
        }

        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public T GetValueOrDefault<T>(string Key, T defaultValue)
        {
            T value;

            // If the key exists, retrieve the value.
            if (settings.Values.ContainsKey(Key))
            {
                value = (T)settings.Values[Key];
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }
            return value;
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            //settings.Save();
        }


        /// <summary>
        /// Propriedade para o campo NFC.
        /// </summary>
        public bool NFCSetting
        {
            get
            {
                return GetValueOrDefault<bool>(NFCSettingKeyName, NFCSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(NFCSettingKeyName, value))
                {
                    Save();
                }
            }
        }

        /// <summary>
        /// Propriedade para o campo GPS.
        /// </summary>
        public bool GPSSetting
        {
            get
            {
                return GetValueOrDefault<bool>(GPSSettingKeyName, GPSSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(GPSSettingKeyName, value))
                {
                    Save();
                }
            }
        }

        /// <summary>
        /// Propriedade para os Radio de predios.
        /// </summary>
        public bool Predio9Setting
        {
            get
            {
                return GetValueOrDefault<bool>(Predio9SettingKeyName, Predio9SettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(Predio9SettingKeyName, value))
                {
                    Save();
                }
            }
        }

        /// <summary>
        /// Propriedade para os Radio de predios.
        /// </summary>
        public bool Predio16Setting
        {
            get
            {
                return GetValueOrDefault<bool>(Predio16SettingKeyName, Predio16SettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(Predio16SettingKeyName, value))
                {
                    Save();
                }
            }
        }

        /// <summary>
        /// Propriedade para os Radio de predios.
        /// </summary>
        public bool Predio23ASetting
        {
            get
            {
                return GetValueOrDefault<bool>(Predio23ASettingKeyName, Predio23ASettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(Predio23ASettingKeyName, value))
                {
                    Save();
                }
            }
        }

        /// <summary>
        /// Propriedade para os Radio de predios.
        /// </summary>
        public bool Predio23BSetting
        {
            get
            {
                return GetValueOrDefault<bool>(Predio23BSettingKeyName, Predio23BSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(Predio23BSettingKeyName, value))
                {
                    Save();
                }
            }
        }

        /// <summary>
        /// Propriedade para guardar o usuário.
        /// </summary>
        public string UsuarioSetting
        {
            get
            {
                return GetValueOrDefault<string>(UsuarioSettingKeyName, UsuarioSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(UsuarioSettingKeyName, value))
                {
                    Save();
                }
            }
        }

        /// <summary>
        /// Propriedade para guardar a senha.
        /// </summary>
        public string SenhaSetting
        {
            get
            {
                return GetValueOrDefault<string>(SenhaSettingKeyName, SenhaSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(SenhaSettingKeyName, value))
                {
                    Save();
                }
            }
        }

        /// <summary>
        /// Propriedade para guardar a foto.
        /// </summary>
        public byte[] FotoSetting
        {
            get
            {
                return GetValueOrDefault<byte[]>(FotoSettingKeyName, FotoSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(FotoSettingKeyName, value))
                {
                    Save();
                }
            }
        }

        /// <summary>
        /// Propriedade para o último checkin do usuário
        /// </summary>
        public string UltimoCheckin
        {
            get
            {
                return GetValueOrDefault<string>(LastCheckinSettingKeyName, LastCheckinSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(LastCheckinSettingKeyName, value))
                {
                    Save();
                }
            }
        }

        /// <summary>
        /// Propriedade para o horário de checkin do usuário pelo GPS
        /// </summary>
        public string HorarioEntradaGPS
        {
            get
            {
                return GetValueOrDefault<string>(HorarioEntradaSettingKeyName, HorarioEntradaSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(HorarioEntradaSettingKeyName, value))
                {
                    Save();
                }
            }
        }

        /// <summary>
        /// Propriedade para o horário de saída do usuário pelo GPS
        /// </summary>
        public string HorarioSaidaGPS
        {
            get
            {
                return GetValueOrDefault<string>(HorarioSaidaSettingKeyName, HorarioSaidaSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(HorarioSaidaSettingKeyName, value))
                {
                    Save();
                }
            }
        }
    }
}
